package playing_with_arrays;


import java.util.Arrays;

import org.junit.Ignore;
import org.junit.Test;
import org.perf4j.StopWatch;
import org.perf4j.log4j.Log4JStopWatch;

import util.TestTools;

public final class ArrayPerformanceTests 
{
	private final StopWatch stopWatch;
	
	private static final int TEST_RUNS      = TestTools.TEST_RUNS;
	private static final int TEST_DATA_SIZE = TestTools.TEST_DATA_SIZE;
	
	public ArrayPerformanceTests()
	{
		this.stopWatch = new Log4JStopWatch();
	}
	
	
	@Test
	public void testArrayCopyWithIteration()
	{
		final String tag = "testArrayCopyWithIteration";
		
		int[] srcArr;
		
		for(int r = 0; r < TEST_RUNS; r++)
		{
			srcArr = TestTools.createTestData();
			this.stopWatch.start(tag);
			
			//------------------------------
			int   srcLength = srcArr.length;
			int[] destArr   = new int[srcLength];

			for(int i = 0; i < srcLength; i++)
			{
			   destArr[i] = srcArr[i];
			}
			//-------------------------------
			
			this.stopWatch.stop(tag);
			
			System.out.println(destArr[destArr.length - 1]);
		}
	}
	
	@Test
	public void testArrayCopyWithSystemArrayCopy()
	{
		final String tag = " testArrayCopyWithSystemArrayCopy";
		
		int[] srcArr;
		
		for(int r = 0; r < TEST_RUNS; r++)
		{
			srcArr = TestTools.createTestData();
			this.stopWatch.start(tag);
			
			//------------------------------
			int   srcLength = srcArr.length;
			int[] destArr   = new int[srcLength];

			System.arraycopy(srcArr, 0, destArr, 0, srcLength);
			//-------------------------------
			
			this.stopWatch.stop(tag);
			
			System.out.println(destArr[destArr.length - 1]);
		}
	}
	
	
	
	@Test
	public void testForwardSequentialArrayAccess()
	{
		final String tag = "testForwardSequentialArrayAccess";
		
		int[] srcArr;
		int[] indexArr;
		
		int count = 0;
		
		for(int r = 0; r < TEST_RUNS; r++)
		{
			srcArr   = TestTools.createTestData();
			indexArr = TestTools.createForwardSequentialAccessIndexArray();
			
			this.stopWatch.start(tag);
			
			//------------------------------
			for(int i = 0; i < TEST_DATA_SIZE; i++)
			{
				if(srcArr[indexArr[i]] > 0)
				{
					count++;
				}
			}
			//-------------------------------
			
			this.stopWatch.stop(tag);
			
			System.out.println(count);
		}
	}
	
	@Test
	public void testBackwardSequentialArrayAccess()
	{
		final String tag = "testBackwardSequentialArrayAccess";
		
		int[] srcArr;
		int[] indexArr;
		
		int count = 0;
		
		for(int r = 0; r < TEST_RUNS; r++)
		{
			srcArr   = TestTools.createTestData();
			indexArr = TestTools.createBackwardSequentialAccessIndexArray();
			
			this.stopWatch.start(tag);
			
			//------------------------------
			for(int i = 0; i < TEST_DATA_SIZE; i++)
			{
				if(srcArr[indexArr[i]] > 0)
				{
					count++;
				}
			}
			//-------------------------------
			
			this.stopWatch.stop(tag);
			
			System.out.println(count);
		}
	}
	
	@Test
	public void testRandomArrayAccess()
	{
		final String tag = "testRandomArrayAccess";
		
		int[] srcArr;
		int[] indexArr;
		int count = 0;
		
		for(int r = 0; r < TEST_RUNS; r++)
		{
			srcArr   = TestTools.createTestData();
			indexArr = TestTools.createRandomAccessIndexArray();
			
			this.stopWatch.start(tag);
			
			//------------------------------
			for(int i = 0; i < TEST_DATA_SIZE; i++)
			{
				if(srcArr[indexArr[i]] > 0)
				{
					count++;
				}
			}
			//-------------------------------
			
			this.stopWatch.stop(tag);
			
			System.out.println(count);
		}
	}
}
